Frequently Asked Questions
Frequently Asked Questions (FAQ) About Java Collectionsβ
This section answers common questions developers have about the Java Collections Framework.
General Questions
Q1: What is the Java Collections Framework?β
Answer:
The Java Collections Framework is a unified architecture for
representing and manipulating collections of objects.
It provides:
- Interfaces (
List,Set,Map,Queue) - Implementations (
ArrayList,HashSet,HashMap, etc.) - Algorithms for searching, sorting, and manipulation.
Q2: Why should I use collections instead of arrays?β
Collections provide several advantages:
- Dynamic resizing
- Rich APIs
- Builtβin algorithms
- Generics (type safety)
- Thread-safe alternatives
Arrays have fixed size and limited utility methods.
Q3: What are the core interfaces of the Collections Framework?β
Core interfaces include:
CollectionListSetMapQueueDeque
Collection is the root interface for most structures except Map.
Implementation Questions
Q4: When should I use ArrayList vs LinkedList?β
Use ArrayList when:
- Frequent reads
- Random access
- Memory efficiency
Use LinkedList when:
- Frequent insertions/removals at ends
In most real applications, ArrayList performs better.
Q5: What is the difference between HashMap and TreeMap?β
| Feature | HashMap | TreeMap |
|---|---|---|
| Ordering | No order | Sorted |
| Performance | O(1) | O(log n) |
| Structure | Hash table | Redβblack tree |
Q6: Can a Map contain null keys or values?β
HashMapβ allows 1 null key and multiple null valuesLinkedHashMapβ same behaviorTreeMapβ null keys not allowed
Q7: What is the difference between HashSet and TreeSet?β
| Feature | HashSet | TreeSet |
|---|---|---|
| Ordering | None | Sorted |
| Performance | O(1) | O(log n) |
| Implementation | HashMap | TreeMap |
Thread Safety Questions
Q8: Are Java collections thread-safe?β
Most collections are not thread-safe.
Examples:
ArrayListHashMap
Thread-safe alternatives:
ConcurrentHashMapCopyOnWriteArrayListBlockingQueue
Q9: Difference between synchronizedList and CopyOnWriteArrayList?β
| Feature | synchronizedList | CopyOnWriteArrayList |
|---|---|---|
| Locking | Uses synchronized methods | No locking |
| Writes | Normal | Creates new array |
| Best Use | Moderate concurrency | Read-heavy systems |
Q10: What is a BlockingQueue?β
A BlockingQueue blocks threads when:
- Queue is empty (consumer waits)
- Queue is full (producer waits)
Examples:
ArrayBlockingQueueLinkedBlockingQueuePriorityBlockingQueue
Used in producer-consumer systems.
Streams and Functional Programming
Q11: What is the Streams API?β
The Streams API (Java 8) allows functional processing of collections.
Examples of operations:
filtermapsortedreducecollect
Q12: Difference between intermediate and terminal operations?β
Intermediate operations:
- Return another stream
- Lazy evaluation
Examples:
filtermapsorted
Terminal operations:
- Produce a result
- Trigger execution
Examples:
collectforEachcount
Q13: When should I use parallel streams?β
Use parallel streams when:
- Dataset is large
- Operation is CPU intensive
- Task can be split into independent subtasks
Avoid for:
- small datasets
- IO operations
Memory and Performance
Q14: How can I reduce memory usage in collections?β
Strategies:
- Preallocate capacity
- Use primitive collections
- Avoid unnecessary boxing
- Trim unused capacity
Libraries:
- FastUtil
- Eclipse Collections
Q15: What does trimToSize() do?β
trimToSize() reduces an ArrayList's internal array capacity to match
its size.
Example:
ArrayList<String> list = new ArrayList<>(100);
list.add("A");
list.trimToSize();
This releases unused memory.
Q16: What is a weak reference in collections?β
Weak references allow objects to be garbage collected if no strong references exist.
Example:
WeakHashMap
Keys can be automatically removed by the garbage collector.
Advanced Topics
Q17: How do I create immutable collections?β
Java 9 introduced factory methods:
List.of() Set.of() Map.of()
These create unmodifiable collections.
Q18: What is a multimap?β
A multimap maps one key to multiple values.
Example:
key β [value1, value2, value3]
Library support:
- Guava
Multimap - Eclipse Collections
Q19: How do I implement a custom collection?β
Extend helper classes like:
AbstractCollectionAbstractListAbstractSet
Then implement required methods such as:
size()iterator()add()
Troubleshooting
Q20: Why do I get ConcurrentModificationException?β
This happens when a collection is modified during iteration.
Example:
for(String s : list){
list.remove(s);
}
Solution:
Use iterator removal:
Iterator<String> it = list.iterator();
while(it.hasNext()){
if(condition){
it.remove();
}
}
Q21: Why does TreeMap or TreeSet throw NullPointerException?β
They require elements to be comparable.
null cannot be compared, so it is not allowed.
Q22: Why is my HashMap slow?β
Possible reasons:
- Poor
hashCode()implementation - Too many collisions
- Large load factor
- Excessive resizing
Ensure equals() and hashCode() are implemented correctly.
This FAQ section helps clarify many of the common misconceptions and practical issues developers face when using Java collections.